<?php
//create short variable names
$tireqty= $_POST['tireqty'];
$oilqty= $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$find = $_POST['find'];

$address = $_POST['address'];
$DOCUMENT_ROOT= $_SERVER['DOCUMENT_ROOT'];
$date=date('H:i,jS F Y');
?>
<html>
<head>
<title>Bob's Auto Parts-Order Result</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at ";
echo date('H:i,jS F Y');
echo "</p>";


$totalqty = 0;
$totalqty = $tireqty+$oilqty+$sparkqty;

if($totalqty==0){
  echo "You did not order anything on the previous page!<br/>";
}else{
  if($tireqty>0)
    echo $tireqty." tires<br/>";
  if($oilqty>0)
    echo $oilqty." bottles of oil<br/>";
  if($sparkqty>0)
    echo $sparkqty." spark plugs<br/>";
}


echo "Items ordered:".$totalqty."<br/>";
$totalamount = 0.00;

define('TIREPRICE',500);
define('OILPRICE',120);
define('SPARKPRICE',20);

$totalamount=$tireqty*TIREPRICE+$oilqty*OILPRICE+$sparkqty*SPARKPRICE;
echo "Subtotal:$".number_format($totalamount,2)."<br/>";
$taxrate=0.10;
$totalamount=$totalamount*(1+$taxrate);
echo "Total including tax:$".number_format($totalamount,2)."<br/>";

echo "<p>Address to ship to is: ".$address."</p>";

$outputstring=$date."\t".$tireqty."tires \t".$oilqty."oil \t".$sparkqty."spark plugs \t\$".$totalamount."\t".$address."\n";
// echo $outputstring;      
// open file for appending
@ $fp=fopen("$DOCUMENT_ROOT/bob/orders/order.txt",'ab');

flock($fp,LOCK_EX);
if(!$fp){
	echo "<p><strong>Your order could not be processed at this time. please try again later.</strong></p>";
	exit;
}
fwrite($fp,$outputstring,strlen($outputstring));
flock($fp,LOCK_UN);
fclose($fp);
echo "<p>Order written.</p>";
?>
</body>
</html>
